package com.hxt.framework.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: CommonUtil
 * @Description: TODO
 * @author: liwei
 * @date: 2016年5月23日 下午2:56:20
 */
public class CommonUtil {

	/**
	 * @Title: MD5
	 * @Description: MD5加密
	 * @param s
	 * @return
	 * @return: String
	 */
	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static boolean isNumeric(String str){
	    Pattern pattern=Pattern.compile("[0-9]*");
	    Matcher isNum=pattern.matcher(str);
	    if(isNum.matches()){
	        return true;
	    }
	    return false;
	}
	
	public static boolean isFullNumeric(String str){
	    boolean strResult = str.matches("-?[0-9]+.*[0-9]*");
        if(strResult){
            return true;
        }
        return false;
    }
	
	
	public static String printStackTraceToString(Throwable aThrowable) {
        StringWriter sw = new StringWriter();
        aThrowable.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }
	
	
	public static String removeBlank(String resource, char ch) {
        StringBuffer buffer = new StringBuffer();
        int position = 0;
        char currentChar;

        while (position < resource.length()) {
            currentChar = resource.charAt(position++);
            // 如果当前字符不是要去除的字符，则将当前字符加入到StringBuffer中
            if (currentChar != ch)
                buffer.append(currentChar);
        }
        return buffer.toString();
    }
	
	public static void main(String[] args) {
		System.out.println(MD5("akakkak").length());
	}
}
